import { defineConfig } from 'vite'
import path from "node:path";
import react from '@vitejs/plugin-react-swc'
import viteCompression from 'vite-plugin-compression'
import qiankun from "vite-plugin-qiankun";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const isDev = mode === 'development';
  return {
    plugins: [
      // 2. 引用qiankun插件，需要注意，这里的microReact是子应用名称，需要和主应用中注册的子应用名称一致
      qiankun("microjz-react", {
        useDevMode: true,
      }),
      // 3. react()插件会跟vite-plugin-qiankun插件冲突，所以需要判断是否是开发环境
      !isDev &&
      react(),
      {
        ...viteCompression(),
        apply: 'build',
      },
    ],
    base: isDev ? "/" : 'http://xxx.com/', // /micro-react/
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "~antd": "antd"
      }
      // [
      //   {
      //     "@": path.resolve(__dirname, "./src"),
      //   },
      //   // {
      //   //   find:/^~/,
      //   //   replacement:'',
      //   // }
      // ]
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true, // 支持内联 JavaScript
          modifyVars: { // 更改主题
            "@ant-prefix": "formily-editor"
          }
        }
      }
    },
    build: {
      rollupOptions: {
        external: ["monaco-editor", "vite-plugin-monaco-editor"],
        output: {
          manualChunks: {
            react: ['react', 'react-dom', 'react-router-dom'], //'zustand'
            // antd: ['antd'],
          },
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        },
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8080,
      open: true,
      proxy: {
        '/api': {
          target: '要代理的地址',
          changeOrigin: true,
          ws: true,
          rewrite: (path: string) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})
